import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';

import { PromotionShareService } from '../promotion-share.service';

@Component({
  selector: 'app-game-new-recommend',
  templateUrl: './game-new-recommend.component.html',
  styleUrls: ['./game-new-recommend.component.scss']
})
export class GameNewRecommendComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '游戏名称', valueKey: 'name', value: '' }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0,
    pages: 0
  };

  // 应用选择
  isSelectVisible = false;
  // addGameIds: Array<any> = [];

  constructor(
    private msg: NzMessageService,
    private services: PromotionShareService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    const params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data}: any = await this.services.getNewGameRecommendData(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total, pages} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
      this.tableParams.pages = pages || 0;
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page=  1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 添加游戏推荐
  async submitGameData(ids: any) {
    const params = {
      gameIds: ids.join(',')
    }
    const {code, data}: any = await this.services.addRecommendGameData(params);

    if (code === 0) {
      this.msg.success('操作成功');
      this.tableParams.page = 1;
      this.loadTable();
    }
  }

  // 删除新游推荐
  async deleteGameRecommend(item: any) {
    const {code, data}: any = await this.services.deleteRecommendGameData({id: item.id});
    if (code === 0) {
      this.msg.success('删除成功');
      this.tableParams.page = 1;
      this.loadTable();
    }
  }

  /**
   * 上下移
   * @param item 
   * @param type 1：上 2：下
   */
  async moveGameRecommend(item: any, type: number) {
    const params = {
      id: item.id,
      type
    }
    const {code, data}: any = await this.services.moveRecommendGameData(params);

    if (code === 0) {
      this.msg.success('操作成功');
      this.tableParams.page = 1;
      this.loadTable();
    }
  }

  // disabled move
  disabledMoveBtn(index?: number): boolean {
    if (this.tableParams.page == this.tableParams.pages && index == (this.tableParams.tableData.length - 1)) {
      return true
    } else {
      return false
    }
  }
}
